package com.lanou.moving.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.moving.R;
import com.lanou.moving.entity.RankingList;

import java.util.List;

import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by dllo on 15/10/22.
 */
public class RankingListAdapter extends BaseAdapter {
    private Context context;
    private List<RankingList> rankingLists;
    private List<RankingList.RefsEntity> refsEntityLists;
    private RankingList.RefsEntity refsEntity;
    // 拼接字符串
    private String first;
    private String second;
    private String third;

    public RankingListAdapter(Context context,
                              List<RankingList> rankingLists, String name) {
        this.context = context;
        this.rankingLists = rankingLists;
        if (name.equals("官方榜")) {
            refsEntityLists = rankingLists.get(0).getRefs();
        }
        if (name.equals("特色榜")) {
            refsEntityLists = rankingLists.get(1).getRefs();
        }
        if (name.equals("全球榜")) {
            refsEntityLists = rankingLists.get(2).getRefs();
        }
    }

    @Override
    public int getCount() {
        return refsEntityLists != null && refsEntityLists.size() > 0 ? refsEntityLists.size() : 0;
    }

    @Override
    public RankingList.RefsEntity getItem(int position) {
        return refsEntityLists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.ranking_list_item, null);
            holder = new ViewHolder();
            holder.rankingList_picture = (ImageView) convertView.findViewById(R.id.rankingList_picture);
            holder.rankingList_title = (TextView) convertView.findViewById(R.id.rankingList_title);
            holder.rankingList_first = (TextView) convertView.findViewById(R.id.rankingList_first);
            holder.rankingList_second = (TextView) convertView.findViewById(R.id.rankingList_second);
            holder.rankingList_third = (TextView) convertView.findViewById(R.id.rankingList_third);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        refsEntity = getItem(position);

        if (refsEntity != null) {
            if (refsEntity.getSongs().size() == 3) {
                Picasso.with(context).load(refsEntity.getImage().getPic()).into(holder.rankingList_picture);
                holder.rankingList_title.setText(refsEntity.getTitle());
                first = "1." + refsEntity.getSongs().get(0).getName() + "-"
                        + refsEntity.getSongs().get(0).getSingerName();
                holder.rankingList_first.setText(first);
                second = "2." + refsEntity.getSongs().get(1).getName() + "-"
                        + refsEntity.getSongs().get(1).getSingerName();
                holder.rankingList_second.setText(second);
                third = "3." + refsEntity.getSongs().get(2).getName() + "-"
                        + refsEntity.getSongs().get(2).getSingerName();
                holder.rankingList_third.setText(third);
            } else {
                {
                    Picasso.with(context).load(refsEntity.getImage().getPic()).into(holder.rankingList_picture);
                    holder.rankingList_title.setText(refsEntity.getTitle());
                    first = "1." + refsEntity.getSongs().get(0).getName() + "-"
                            + refsEntity.getSongs().get(0).getSingerName();
                    holder.rankingList_first.setText(first);
                }
            }
        }
        return convertView;
    }
    private class ViewHolder {
        private ImageView rankingList_picture;
        private TextView rankingList_title;
        private TextView rankingList_first;
        private TextView rankingList_second;
        private TextView rankingList_third;
    }
}
